import React from 'react'
import {AsyncStorage, FlatList, Image, Switch, Text, TouchableOpacity, View} from 'react-native'
import tuisong from '../images/tuisong.png'
import qingchu from '../images/qingchu.png'
import banbenjiance from '../images/updata.png'
import banben from '../images/banben.png'
import tuichu from '../images/chu.png'
import BasePage from '../BasePage';
import {NavigationBar, Toast} from 'teaset';
import DeviceInfo from 'react-native-device-info'

import LoginPage from '../LoginPage'

var imagesArr=[tuisong,qingchu,banbenjiance,banben,tuichu]
export default class SettingsPage extends BasePage{
    constructor(props){
        super(props);
        this.state={
            notification:true,
        }
    }
    componentDidMount(){

    }
    renderNavigationBar() {
        return (
            <NavigationBar
                style={{backgroundColor: 'white',}}

                title={
                    <View style={{
                        flexDirection: 'row',
                        justifyContent: 'center',
                        alignSelf: 'center',

                    }}>
                        <Text style={{
                            fontSize: 20,
                            color: '#333333'
                        }}>设置</Text>
                    </View>}

                leftView={<TouchableOpacity
                    onPress={() => {


                        this.navigator.pop();


                        // DeviceEventEmitter.emit('my', {text: '刷新'})

                    }}
                >
                    <Image style={{width: 15, height: 15, marginLeft: 20}} source={require('../images/btn_back.png')}/>

                </TouchableOpacity>}

            />

        );
    }

    renderPage(){
        return (
            <View style={{flex:1, backgroundColor:'#f2f2f2',marginTop:70}}>
                <FlatList
                    data={[{
                        key:1,
                        name:'推送'
                    },{
                        key:2,
                        name:'清除缓存'
                    },{
                        key:3,
                        name:'版本检测'
                    },{
                        key:4,
                        name:'版本'
                    },{
                        key:5,
                        name:'退出'
                    }]}
                    renderItem={this._renderItem}
                    keyExtractor={this._keyExtractor}
                />
            </View>
        );
    }
    _renderItem=({item,index})=>{
        var rightView;
        var notification =this.state.notification
        if(index==1||index==2||index==4){
            rightView= <Image source={require('../images/go.png')} style={{ position:'absolute', marginTop:10, right:10, width:15, height:15}}/>
        }else if(index==3){
            rightView= <Text style={{position:'absolute',right:20, marginTop:20}}>1.0</Text>
        }else{
            rightView= <Switch value={this.state.notification} style={{position:'absolute', right:15, marginTop:10}} onValueChange={()=>{
                this.setState((previousState)=>({
                    notification:!previousState.notification
                }));
            }}/>
        }
        return(
            <TouchableOpacity style={{height:50, flexDirection:'row', marginTop:10 ,backgroundColor:'white'}}
            onPress={()=>{
                if(index==1){
                    Toast.message('正在清理...')
                    setTimeout(()=>{
                        Toast.message('清除完成');
                    },2000)
                    
                }else if(index==2){
                    Toast.message('正在检测...')
                    setTimeout(()=>{
                        Toast.message('已是最新版本');
                    },2000)

                }else  if (index == 4) {
                    var person = {
                        name: '',
                        islond: 0,
                        headurl:''
                    }
                    var jsonStr = JSON.stringify(person)


                    AsyncStorage.setItem('person', jsonStr, function (error) {
                        if (error) {
                            // alert('存储失败')
                        } else {
                            // alert('存储完成')
                        }
                    })

                    this.navigator.resetTo({
                        view: <LoginPage/>
                    })
                }
            }}>
                <Image style={{marginTop:10, marginLeft:15, width:30, height:30 }} source={imagesArr[index]}/>
                <Text style={{marginTop:20, marginLeft:15}}>{item.name}</Text>
                {rightView}
            </TouchableOpacity>
        );
    }
    _keyExtractor=({item,index})=>{
        return index;
    }
}